package com.security.jjwt;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;

public class Demo {
    public static void main(String[] args) {
//        创建token
        JwtBuilder jwtBuilder = Jwts.builder();
        //声明标识{"jti":"88888"}
        jwtBuilder.setId("4444");
        //主体 用户{“sub”,"rose"}
        jwtBuilder.setSubject("rose");
        //创建日期 {“ita”:"xx"}
        jwtBuilder.setIssuedAt(new Date());
        jwtBuilder.setIssuer("111");
//        自定义声明
        jwtBuilder.claim("roles","admin");
        jwtBuilder.claim("logo","xxx.jpg");

        //签名
        jwtBuilder.signWith(SignatureAlgorithm.HS256,"xxxxxx");
//        设置失效时间
        jwtBuilder.setExpiration(new Date(System.currentTimeMillis() + 60 *1000));
        String token = jwtBuilder.compact();
//        解析token 获取负载中声明的对象
        Claims claims = Jwts.parser()
                .setSigningKey("xxxxxx")
                .parseClaimsJws(token)
                .getBody();
        String id = claims.getId();
        String subject = claims.getSubject();
        String issuer = claims.getIssuer();
        Date expiration = claims.getExpiration();
        Date issuedAt = claims.getIssuedAt();
//        获取自定义声明
        Object roles = claims.get("roles");
        Object logo = claims.get("logo");

    }
}
